package org.example.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.common.R;
import org.example.pojo.entity.WmNews;
import org.example.pojo.vo.WmNewsVo;
import org.example.service.IWmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 自媒体图文内容信息表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-01-26
 */
@CrossOrigin(maxAge = 3600)
@RestController
@RequestMapping("/front")
public class WmNewsController {

    @Autowired
    private IWmNewsService wmNewsService;

    /**
     * 获取文章列表数据
     *
     * @param page
     * @param size
     * @param channelId
     * @param title
     * @return
     */
    @GetMapping("news/list")
    public R list(@RequestParam Integer page,
                  @RequestParam Integer size,
                  @RequestParam(required = false) String channelId,
                  @RequestParam(required = false) String title) {
        Page pages = wmNewsService.listNews(page, size, channelId, title);
        return R.ok(pages);
    }

    /**
     * 获取文章详情
     *
     * @param newsId
     * @return
     */
    @GetMapping("news/detail")
    public R detail(HttpServletRequest request, @RequestParam Integer newsId) {
        WmNewsVo news = wmNewsService.getNewsDetail(newsId, request);
        return R.ok(news);
    }


    /**
     * 获取头条热榜数据10条
     *
     * @return
     */
    @GetMapping("news/hot")
    public R hot() {
        Page page = new Page();
        page.setCurrent(1);
        page.setSize(10);
        Page<WmNews> pages = wmNewsService.page(page, null);
        List<WmNews> records = pages.getRecords();
        return R.ok(records);
    }


    /**
     * 获取头条热榜数据10条
     *
     * @return
     */
    @GetMapping("news/random")
    public R random() {
        Page page = new Page();
        page.setCurrent(1);
        page.setSize(10);
        Page<WmNews> pages = wmNewsService.page(page, null);
        List<WmNews> records = pages.getRecords();
        return R.ok(records);
    }


    /**
     * 发布文章
     *
     * @param wmNews
     * @return
     */
    @PostMapping("/publish")
    public R publish(@RequestBody WmNews wmNews) {
        wmNewsService.publish(wmNews);
        return R.ok("发布文章成功");
    }


    /**
     * 获取个人或收藏的作品
     *
     * @param page
     * @param size
     * @param type
     * @return
     */
    @GetMapping("/person/collect")
    public R person(@RequestParam int page, @RequestParam int size, String type,String searchParam) {
        return wmNewsService.person(page, size, type,searchParam);
    }


}
